﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TR.AppService.Dto;
using TR.AppService.Dto.RKGLDto;
using TR.Domain.Entity;
using TR.Domain.Entity.RK;
using TR.Domain.Repository.RKGLRepository;

namespace TR.AppService.AppService.RKGL
{
    public class Wh_Inbill_hAppService : IWh_Inbill_hAppService
    {
        private readonly IMapper mapper;
        private readonly IWh_Inbill_hRepository iwih;
        public Wh_Inbill_hAppService(IMapper mapper, IWh_Inbill_hRepository iwih)
        {
            this.mapper = mapper;
            this.iwih = iwih;
        }

        #region 新增页面功能

       
        //关联单号查询进货信息
        public async Task<DtoFenYe<GuanlianJHDH>> GLDHFenYeSelect(int mys, int dqy, string? dqsj, string? jssj, GLDHTjDto tj)
        {
            var listFenYe = await iwih.GLDHFenYeSelectAsync(mys, dqy, dqsj, jssj, tj);
            var glzts = await iwih.Ztshu();
            var result = new DtoFenYe<GuanlianJHDH>()
            {
                mys = mys,
                dqy = dqy,
                list = listFenYe,
                zts = glzts,
            };
            return result;
        }
        
        //根据进货单号查询商品
        public Task<List<JHDHchaHP>> JHDHChahp(string buy_code, HPTj tj)
        {
            return iwih.JHDHchahp(buy_code, tj);
        }

        //添加入库数据
        public async Task<bool> widInsert(wh_Inbill_hInsertDto wihi, List<wh_Inbill_dInsertDto> widi)
        {
            wh_Inbill_h wihst = mapper.Map<wh_Inbill_h>(wihi);
            var widiInsert = mapper.Map<List<wh_Inbill_d>>(widi);
            return await iwih.widInsert(wihst, widiInsert);
        }

        //仓库下拉框查询
        public Task<List<bs_WareHouse>> CKXLKSelect()
        {
            return iwih.CKXLKSelect();
        }
        #endregion

        #region 主页面查询功能
        //入库管理主表
        public async Task<DtoFenYe<RKGL_SelectZB>> RKGLSelect(int mys, int dqy, RKGLZbTjCx tj)
        {
            var listFenye = await iwih.RKGL_SelectZB(mys, dqy, tj);
            var zbzts = await iwih.RKZtShu();
            var result = new DtoFenYe<RKGL_SelectZB>
            {
                mys = mys,
                dqy = dqy,
                list = listFenye,
                zts = zbzts
            };
            return result;
        }

        //批量修改状态
        public async Task<bool> updateState(List<int> Bian, char zt)
        {
            return await iwih.UpdateState(Bian, zt);
        }

        //根据仓库编号查询库位
        public async Task<List<bs_Ware_Area>> SelectWare(string ware_code)
        {
            return await iwih.SelectWare(ware_code);
        }
        //批量逻辑删除
        public async Task<bool> RKGLDelete(List<int> isdel)
        {
            return await iwih.RKGLDelect(isdel);
        }
        //单条逻辑删除
        public async Task<bool> RKGLDel(int Bian)
        {
            return await iwih.RKGLDel(Bian);
        }

        //入库批量审核
        public async Task<bool> SHUpdate(List<int> Bian, char state, string sureman, string suredate, string remark)
        {
            return await iwih.SHUpdate(Bian, state, sureman, suredate, remark);
        }

        //批量撤销
        public async Task<bool> CXUpdate(List<int> Bian)
        {
            return await iwih.CXUpdate(Bian);
        }

        #endregion

        #region 编辑页面功能
        //根据编号查询信息
        public async Task<wh_Inbill_h> RKBhSelect(string bh)
        {
            return await iwih.RKBhSelect(bh);
        }

        //编辑入库明细查询
        public async Task<DtoFenYe<RKMXXG>> RKMXXGSelect(int mys, int dqy, string tj)
        {
            var listFenYe = await iwih.RKMXXGSelect(mys, dqy, tj);
            var mxzts = await iwih.MXCount();
            var result = new DtoFenYe<RKMXXG>()
            {
                mys = mys,
                dqy = dqy,
                list = listFenYe,
                zts = mxzts
            };
            return result;
        }

        //入库信息修改
        public async Task<bool> RKUpdate(wh_Inbill_hUpdateDto jcxx, List<wh_Inbill_dUpdateDto> rkmx)
        {
            wh_Inbill_h jc = mapper.Map<wh_Inbill_h>(jcxx);
            var mx = mapper.Map<List<wh_Inbill_d>>(rkmx);
            return await iwih.RKUpdate(jc, mx);

        }
        #endregion
    }
}
